#include "Common.hpp"
#include "Protocol.hpp"
#include "Socket.hpp"
#include <cstdlib>
#include <iostream>
#include <memory>
#include <mutex>
#include <string>
using namespace SocketModule;
void Usage(std::string proc) {
    std::cout << "The usage is " << proc << " + ip + port" << endl; 
}
void GetData(int* x, int* y, char* oper) {
    std::cout << "Please Enter x: ";
    std::cin >> *x;
    std::cout << "Please Enter y: ";
    std::cin >> *y;
    std::cout << "Please Enter oper: ";
    std::cin >> *oper;
}
int main(int argc, char* argv[]) {
    if(argc != 3) {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }
    std::string ip = argv[1];
    uint16_t port = std::stoi(argv[2]);
    std::shared_ptr<Socket> client = std::make_shared<TcpSocket>();
    client ->BuildTcpClientSocketMethod();
    if(client->Connect(ip, port) != 0) {
        // 失败了
        std::cerr << "connect error" << std::endl;
        exit(CONNECT_ERR);
    }
    std::unique_ptr<Protocol> protocol = std::make_unique<Protocol>();
    std::string buffer;
    while (1) {
        int x, y;
        char oper;
        GetData(&x, &y, &oper);
        std::string req = protocol->BuildRequestString(x, y, oper);
        client->Send(req);
        Response resp;
        bool ret = protocol->GetResponse(client, buffer, &resp);
        if(ret == false) {
            break;
        }
        resp.ShowResult();
    }
    client->Close();
    return 0;
}